/**
 * @(#) popMedicalSubtractionMediator.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭6 의료비 공제 팝업창 Logic을 관리 
 */
 package com.skt.common_hr.yearadjustment.view
 {
 	import com.skt.common_hr.common.PsHrAlert;
 	import com.skt.common_hr.common.Variables;
 	import com.skt.common_hr.common.puremvc.CommonMediator;
 	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
 	import com.skt.common_hr.yearadjustment.model.BizNumValidProxy;
 	import com.skt.common_hr.yearadjustment.model.DomainProxy;
 	import com.skt.common_hr.yearadjustment.model.InhabitantsComboProxy;
 	import com.skt.common_hr.yearadjustment.model.MedicalProxy;
 	import com.skt.common_hr.yearadjustment.model.vo.MedicalSubtractionVO;
 	import com.skt.common_hr.yearadjustment.view.components.popMedicalSubtraction;
 	import com.wcl.utils.NumberUtil;
 	
 	import flash.events.Event;
 	import flash.events.MouseEvent; 
 	
 	import mx.collections.ArrayCollection;  
 	import mx.managers.PopUpManager;
 	
 	import org.puremvc.as3.multicore.interfaces.INotification;  
 	
 	 
 	public class popMedicalSubtractionMediator extends CommonMediator
 	{
 		public static const NAME:String = "popMedicalSubtractionMediator";
 		public var v_year:String = "";
 		public var v_percent3:Number = 0;
		[Bindable] public var v_enable:Boolean = true;
		
		public function set v_3percent(v:Number):void
		{
			v_percent3 = v;
			view.PCNT3_F = NumberUtil.NumberFormat(v);
			
			if(Variables.company=='FNU') {
				if(Variables.open == "2" || Variables.open == "3"){
					view.PCNT3_F = "**********";
				}				
			}
			
							 
		}
		
//shgoo 2010.01.12
		//추가, 수정 구분
		private var bAdd:Boolean = true;
				 		
 		/**
 		 * 생성자
 		 * 
 		 * @param
 		 * @return void
 		 * */
 		public function popMedicalSubtractionMediator( viewComponent:Object ):void
 		{
 			super(NAME, viewComponent);
 			view.callLater(initView);
 		}
 		
 		/**
 		 * View를 초기화 한다.
 		 * 	- View에 기본사항을 출력하고 data를 load
 		 * @param
 		 * @return void
 		 * */
 		public function initView():void
 		{ 
 			view.addEventListener(popMedicalSubtraction.POPMEDICALSAVE, onPopMedicalSave);
 			view.addEventListener(popMedicalSubtraction.CancelInput, onCancelInput);
//shgoo 2010.01.11
 			view.btnAdd.addEventListener(MouseEvent.CLICK, onAdd);
 			view.btnModify.addEventListener(MouseEvent.CLICK, onModify);
/////////////////

 			view._isEnable = v_enable;
			
 			loadMedicalList();
 			loadFamily();
 			loadDomain();
 		}
 		
//shgoo 2010.01.11 		
 		//추가버튼 클릭 이벤트
 		private function onAdd(event:Event):void {
			bAdd = true;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 추가
				view.addArray();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}
 		
 		//수정버튼 클릭 이벤트
 		private function onModify(event:Event):void {
			bAdd = false;
			/*
 			if(view.RADIO.selectedValue == "X") {	//국세청 자료 총합으로 입력 인경우
				//항목 수정
				view.onModifyItem();
 			}
 			else {	//국세청 자료 총합 입력이 아닌경우
 				//사업자  번호 유효성 체크
 				var proxy:BizNumValidProxy = facade.retrieveProxy(BizNumValidProxy.NAME) as BizNumValidProxy;
 				proxy.load(view.D_COMCD.text);
 			}
 			*/
 		}

///////////////////
 		
 		//주민등록번호 콤보
 		private function loadFamily():void{
 			var proxy:InhabitantsComboProxy = facade.retrieveProxy(InhabitantsComboProxy.NAME) as InhabitantsComboProxy;
 			proxy.comboload(Variables.yearPernr, v_year);
 			// 			2012.01.03 jt
 			proxy.typeload();
 		}
 		
 		private function loadDomain():void{
 			var proxy:DomainProxy = facade.retrieveProxy(DomainProxy.NAME) as DomainProxy;
 			proxy.loadCodeList(DomainProxy.PKR_MEPCD);
 		}

 		//기본 데이터 조회
 		private function loadMedicalList():void{
 			var proxy:MedicalProxy = facade.retrieveProxy(MedicalProxy.NAME) as MedicalProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		//저장
 		private function onPopMedicalSave(event:Event):void{
 			if(view.totalAmount<v_percent3 && view.totalAmount!=0 ) {
 				PsHrAlert.showMessage("의료비 합계가 총급여액*3% 미만입니다.");
 				return;
 			} 
 			
 			var detailList:Array = new Array();
	 		for each(var rec:MedicalSubtractionVO in view.medicalData)
	 		{
	 			detailList.push(rec);
	 		}
 			
 			var proxy:MedicalProxy = facade.retrieveProxy(MedicalProxy.NAME) as MedicalProxy;
 			proxy.save(Variables.yearPernr, v_year, detailList);
 		}
 		
 		//팝업 닫기
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popMedicalSubtractionMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					YearAdjustmentFacade.LOAD_MEDICAL_LIST_COMPLETE,
					YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED,
					YearAdjustmentFacade.SAVE_MEDICAL_LIST_COMPLETE,
					YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED,
					YearAdjustmentFacade.LOAD_COMBO_INHAB_COMPLETE,
					YearAdjustmentFacade.LOAD_COMBO_INHAB_FAILED,
					YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_COMPLETE,
					YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_FAILED,
					DomainProxy.LOAD_DOMAIN_LIST_COMPLETE,
					DomainProxy.LOAD_DOMAIN_LIST_FAILED,
//					2012.01.04 jt
					YearAdjustmentFacade.LOAD_COMBO_TYPE_COMPLETE,
					YearAdjustmentFacade.LOAD_COMBO_TYPE_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				case YearAdjustmentFacade.LOAD_MEDICAL_LIST_COMPLETE:
					view.medicalData = (note.getBody() as MedicalProxy).medicalData;
					break;
				case YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED:
					trace("YearAdjustmentFacade.LOAD_MEDICAL_LIST_FAILED");
					break;
				case YearAdjustmentFacade.SAVE_MEDICAL_LIST_COMPLETE:
					onCancelInput(null);
					//regMediator();
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					//facade.sendNotification(YearAdjustmentView.CHECK_REQ_MEDICALSUBTRACTION);
					trace("YearAdjustmentFacade.SAVE_DONATION_LIST_COMPLETE:");
					break;
				case YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED:
					trace("YearAdjustmentFacade.SAVE_MEDICAL_LIST_FAILED");
					PsHrAlert.showMessage((note.getBody() as MedicalProxy).msgStr);
					break;
				case YearAdjustmentFacade.LOAD_COMBO_INHAB_COMPLETE:
//shgoo 2010.01.07				
					view.comboInhabitants = (note.getBody() as InhabitantsComboProxy).comboInhabitants;
					//view.comboInhabitants = getValidInhabitants( (note.getBody() as InhabitantsComboProxy).comboInhabitants );
				
					trace("YearAdjustmentFacade.LOAD_COMBO_INHAB_COMPLETE:");
					break;
				case YearAdjustmentFacade.LOAD_COMBO_INHAB_FAILED:
					trace("YearAdjustmentFacade.LOAD_COMBO_INHAB_FAILED");
					break;
//shgoo 2010.01.11					
				case YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_COMPLETE:	//사업자 등록 번호 유효성 체크 성공
					if(bAdd)	//항목 추가
						view.addArray();
					else		//항목 수정
						view.onModifyItem();
					break;
				case YearAdjustmentFacade.LOAD_BIZ_NUM_VALID_FAILED:	//사업자 등록 번호 유효성 체크 에러
					PsHrAlert.showMessage((note.getBody() as BizNumValidProxy).msgStr);
					break;
///////////////////////			
				case DomainProxy.LOAD_DOMAIN_LIST_COMPLETE:
					view.evidenceList = (note.getBody() as DomainProxy).codeList;
					view.evidenceList.addItemAt({CODE:"", TEXT:"증빙코드 선택"}, 0);
					break;
				case DomainProxy.LOAD_DOMAIN_LIST_FAILED:
					break;
//				2012.01.04 jt	
				case YearAdjustmentFacade.LOAD_COMBO_TYPE_COMPLETE:
					view.comboInType = (note.getBody() as InhabitantsComboProxy).comboInType;
					break;	
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
//shgoo 2010.01.07		
		//대상자 주민번호 추출
		private function getValidInhabitants(arrSrc:ArrayCollection):ArrayCollection {
			var arrDest:ArrayCollection = new ArrayCollection();
			
			//주민등록번호 선택 텍 스트 			
			arrDest.addItem(arrSrc.getItemAt(0));
			
			for each ( var rec:Object in arrSrc) {
				if(rec.DPTID == "X") {
					arrDest.addItem(rec);
				}
			}
			
			return arrDest;
		}
///////////////////
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function get view():popMedicalSubtraction
		{
			return viewComponent as popMedicalSubtraction;
		}
	}
 }